package com.walleipt.ak47.services;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.JsonSyntaxException;
import com.walle.ak47.commons.utils.IOUtils;
import com.walle.ak47.commons.utils.StringUtils;
import com.walle.ak47.commons.utils.json.GBuilder;

public abstract class ApiServices {

	private static final Logger LOG = LoggerFactory.getLogger( ApiServices.class )  ;
	private static final String failJson = "{\"result\":\"false\",\"reason\":\"%s\",\"resultData\":null}" ;
	private static final String successJson = "{\"result\":\"true\",\"reason\":null,\"resultData\":%s}" ;
	
	public abstract void process( HttpServletRequest request, HttpServletResponse response) throws Throwable;
	
	public String getString(HttpServletRequest request, HttpServletResponse response) throws Throwable {
		try {
			return IOUtils.toString( request.getInputStream(), "UTF-8")  ;
		} catch (IOException e) {
			this.outFailJson(request, response, "IO异常"); 
			LOG.error( e.getMessage() );
			throw new Exception ( "IO异常,err:"+e.getMessage() ) ;
		}
	}
	
	public <T> T getJsonObject(HttpServletRequest request, HttpServletResponse response, Class<T> classOfT) throws Throwable {
		String body = null ;
		try {
			body = IOUtils.toString( request.getInputStream() , "UTF-8");
			return GBuilder.gson().fromJson( body, classOfT) ;
		} catch (JsonSyntaxException e) {
			this.outFailJson(request, response, "请求报文异常"); 
			LOG.error( "json转换出现异常：param:" + body +",err:"+e.getMessage() );
			throw new Exception ( "json转换出现异常：param:" + body +",err:"+e.getMessage() ) ;
		} catch (IOException e ) {
			LOG.error( e.getMessage() );
		}
		return null ;
	}
	
	public void outJson(HttpServletRequest request, HttpServletResponse response, String result) throws Throwable  {
		byte[] resultBypte = StringUtils.getBytes( result, "UTF-8") ;
		try {
			if (request.getInputStream() != null) {
				if ( resultBypte.length == 0 ) {
					response.setContentLength(0);
				} else {
					response.setContentLength( resultBypte.length );
					response.setContentType("application/json; charset=UTF-8");
					response.getOutputStream().write( resultBypte );
				}
			}
		} catch (IOException e) {
			this.outFailJson(request, response, "IO异常"); 
			LOG.error( e.getMessage() );
			throw new Exception ( "IO异常,err:"+e.getMessage() ) ;
		}
	}
	
	public void outSuccessJson(HttpServletRequest request, HttpServletResponse response, String resultData) throws Throwable {
		byte[] result = StringUtils.getBytes( String.format(successJson, resultData ), "UTF-8") ;
		try {
			if (request.getInputStream() != null) {
				response.setContentLength( result.length );
				response.setContentType("application/json; charset=UTF-8");
				response.getOutputStream().write( result );
				response.getOutputStream().close(); 
			}
		} catch (IOException e) {
			this.outFailJson(request, response, "IO异常"); 
			LOG.error( e.getMessage() );
			throw new Exception ( "IO异常,err:"+e.getMessage() ) ;
		}
	}
	
	public void outFailJson(HttpServletRequest request, HttpServletResponse response, String reason) throws Throwable {
		byte[] result = StringUtils.getBytes( String.format(failJson, reason ), "UTF-8") ;
		try {
			if (request.getInputStream() != null) {
				response.setContentLength( result.length );
				response.setContentType("application/json; charset=UTF-8");
				response.getOutputStream().write( result );
				response.getOutputStream().close(); 
			}
		} catch (IOException e) {
			this.outFailJson(request, response, "IO异常"); 
			LOG.error( e.getMessage() );
			throw new Exception ( "IO异常,err:"+e.getMessage() ) ;
		}
	}
}
